
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'fun/need/list',
        datatype: "json",
        colModel: [
            { label: '招聘ID', name: 'id', index: "id", width: 25, key: true},
            { label: '招聘要求', name: 'demand', width: 100 },
            { label: '岗位类型', name: 'post', width: 45 },
            { label: '招聘部门', name: 'deptName', sortable: false, width: 75 },
			{ label: '需求人数', name: 'needNum',  width: 35},
            { label: '学历要求', name: 'education', width: 50 },
			{ label: '工作年限', name: 'workTime', width: 35 },
            { label: '招聘负责人', name: 'recruitName', width: 50 },
			{ label: '完成状态', name: 'status', width: 40, formatter: function(value, options, row){
				return value === 0 ? 
					'<span class="label label-danger">进行中</span>' :
					'<span class="label label-success">已完成</span>';
			}},
			{ label: '发布时间', name: 'createTime', index: "create_time", width: 85}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        /*	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });*/
            $("#jqGrid").setGridParam().hideCol("id").trigger("reloadGrid");
        }
    });
});
//部门结构树
var dept_ztree;
var dept_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var vm= new Vue({
    el:'#rrapp',
    data:{
        q:{
            post: null
        },
        recruit:{
            deptId:null,
            deptName:null,
            education:null,
        },
        showList: true,
        title:null,
        educations: [{value:'大专'},{value:'本科'},{value:'硕士'},{value:'博士'}],
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.recruit = {deptName:null, deptId:null, education:null};
            vm.getDept();
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";

            vm.getRecruit(id);
            vm.education=vm.recruit.education;
            vm.getDept();
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "fun/need/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            var url = vm.recruit.id == null ? "fun/need/save" : "fun/need/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.recruit),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getRecruit: function(id){
            $.get(baseURL + "fun/need/info/"+id, function(r){
                vm.recruit = r.need;
            });
        },
        getDept: function(){
            //加载部门树
            $.get(baseURL + "sys/dept/list", function(r){
                dept_ztree = $.fn.zTree.init($("#deptTree"), dept_setting , r);
            })
        },
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = dept_ztree.getSelectedNodes();
                    //选择上级部门
                    vm.recruit.deptId = node[0].deptId;
                    vm.recruit.deptName = node[0].name;
                    layer.close(index);
                }
            });
        },
        reload: function () {
           vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'post': vm.q.post,'deptId':''},
                page:page
            }).trigger("reloadGrid");
        },
    }
});
window.addEventListener("message",function(event){
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'deptId':event.data,'post':''},
                page:page
            }).trigger("reloadGrid");

}, false);
